package com.han.nettytestdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.han.nettytestdemo.connector.ImsStateListener;
import com.han.nettytestdemo.connector.PollingUtils;
import com.han.nettytestdemo.connector.communication.ConsumerProduct;
import com.han.nettytestdemo.connector.communication.OrderStorage;
import com.han.nettytestdemo.connector.communication.SocketClientImpl;
import com.han.nettytestdemo.connector.communication.SocketHostSerialInterface;
import com.han.nettytestdemo.connector.netty.intf.MessageClientCallback;

public class MainActivity extends AppCompatActivity implements ImsStateListener {

    private TextView tvLog;
    private SocketHostSerialInterface socketHostSerialInterface;
    private ConsumerProduct consumerProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLog = findViewById(R.id.tv_log);

        socketHostSerialInterface = new SocketClientImpl("192.168.1.27");
        startUsbServer();
        PollingUtils.getInstance().startTime();
        PollingUtils.getInstance().setImsStateListener(this);

    }

    private void startUsbServer() {
        if (consumerProduct == null) {
            consumerProduct = new ConsumerProduct(socketHostSerialInterface, OrderStorage.getInstance());
        }
        if (!consumerProduct.isAlive() || consumerProduct.isInterrupted()) {
            consumerProduct.start();
        }
    }


    @Override
    public void onImsState(String model) {
        runOnUiThread(() -> tvLog.append(model + "\n\n"));

    }
}